<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Home extends \Phpcmf\Home\Module
{

	public function index() {
		$this->_Index();
	}
	//检测帖子是否存在创业金
	public function cunzai(){
		$id = (int)\Phpcmf\Service::L('input')->get('id'); // 帖子id
		$cid = (int)\Phpcmf\Service::L('input')->get('cid'); // 圈子id
		//开始统计
		$chuangyejin_tongji = \Phpcmf\Service::M()->db->table('1_chuangyejin')->where('tieid', $id)->where('quanid', $cid)->countAllResults();
		$chuangyejin_tongji_data = \Phpcmf\Service::M()->db->table('1_chuangyejin')->where('tieid', $id)->where('quanid', $cid)->get()->getRowArray();

        $chuangyejin_data_1 = dr_string2array($chuangyejin_tongji_data['cyjgg']);
		// 循环创业金
		$chuangyejin_data_2 = [];
		if($chuangyejin_data_1){
			foreach ($chuangyejin_data_1 as $key => $value) {
				$chuangyejin_data_2[] = [
					'name' => $value[1],
					'zhifu' => $value[2],
					'shuoming' => $value[3],
				];
			}
		}
		// 检测时间
		if($chuangyejin_tongji_data['jssj'] > time()){
			$jssj = 1;
		}else{
			$jssj = 2;
		}
		//全部申请		
		//统计chuangyejin字段总和
		$sum_result = \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan')
			->where('tieid', $id)
			->where('quanid', $cid)
			->selectSum('chuangyejin')
			->get()
			->getRowArray();
		$chuangyejin_sum = $sum_result ? floatval($sum_result['chuangyejin']) : 0;
		
		//计算剩余创业金（总金额减去已申请金额）
		$total_chuangyejin = floatval($chuangyejin_tongji_data['cyjje']);
		$remaining_chuangyejin = max(0, $total_chuangyejin - $chuangyejin_sum);
		//统计申请人数
		$chuangyejin_renshu = \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan')
			->where('tieid', $id)
			->where('quanid', $cid)
			->countAllResults();
		
		// 用户登录的情况下
		if($this->uid){
			// 检测用户是否购买
			$chuangyejin_tongji_data_1 = \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan')->where('tieid', $id)->where('quanid', $cid)->where('shenqingren', $this->uid)->countAllResults();
		    //统计用户申请到创业金总和
			$uid_sum_result = \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan')
				->where('tieid', $id)
				->where('quanid', $cid)
				->where('shenqingren', $this->uid)
				->selectSum('chuangyejin', 'chuangyejin_total')
				->selectSum('jine', 'jine_total')
				->get()
				->getRowArray();
			// 计算两个字段的总和
			$chuangyejin_total = $uid_sum_result ? floatval($uid_sum_result['chuangyejin_total']) : 0;
			$jine_total = $uid_sum_result ? floatval($uid_sum_result['jine_total']) : 0;
			$uid_chuangyejin_sum = $chuangyejin_total + $jine_total;
		}else{
			$chuangyejin_tongji_data_1 = 0;
			$uid_chuangyejin_sum = 0;
		}

		$chuangyejin_tongji_data_0 = [
			'cyj_id' => $chuangyejin_tongji_data['id'],
			'title' => $chuangyejin_tongji_data['title'],
			'zhuangtai' => $chuangyejin_tongji_data['zhuangtai'],
			'jssj' => dr_date($chuangyejin_tongji_data['jssj'], 'Y-m-d'),
			'cyjje' => $chuangyejin_tongji_data['cyjje'],
			'shuliang' => $chuangyejin_tongji_data['shuliang'],
			'sqsc' => $chuangyejin_tongji_data['sqsc'],
			'zhuangtai' => $chuangyejin_tongji_data['fstatus'],
			'chuangyejin' => $chuangyejin_data_2,
			'jssj_zhi' => $jssj,
			'uid_goumai_shu' => $chuangyejin_tongji_data_1, // 用户购买数次
			'uid_chuangyejin' => $uid_chuangyejin_sum, //用户共申请到创业金
			'chuangyejin_sum' => $chuangyejin_sum, // chuangyejin字段总和
			'chuangyejin_renshu' => $chuangyejin_renshu, // 申请人数
			'chuangyejin_shengyu' => $remaining_chuangyejin, // 剩余创业金

		];
		$this->_json($chuangyejin_tongji, '读取成功', $chuangyejin_tongji_data_0);
	}

}
